<template>
  <div class="add-edit">
    <el-form :model="ruleForm"
             :rules="rules"
             ref="ruleForm"
             label-width="130px"
             class="yuyi-ruleForm">
      <el-form-item label="划控词" prop="delimitationControlWord">
        <el-input size="small" v-model="ruleForm.delimitationControlWord"></el-input>
      </el-form-item>
      <el-form-item label="是否可用" prop="isEnable">
        <el-switch v-model="ruleForm.isEnable"></el-switch>
      </el-form-item>
      <el-form-item label="排序号" prop="orderNo">
        <avue-input-number v-model="ruleForm.orderNo"
                           style="width: 100%"
                           size="small"
                           :precision="0"
                           :min-rows="1"
                           :max-rows="1000"></avue-input-number>
      </el-form-item>
      <el-form-item label="备注" prop="name">
        <el-input size="small" type="textarea" :autosize="autosize" v-model="ruleForm.remark"></el-input>
      </el-form-item>
      <el-form-item label="" prop="name">
        <el-button type="primary" :loading="btnLoading" @click="handleSave" size="small">{{ type === 'add' ? '确定新增' :
          '确定修改'}}
        </el-button>
        <el-button size="small" :loading="btnLoading" @click="handleCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addAuxiliary, editAuxiliary } from '../../../../../api/auxiliary'

export default {
  name: 'auxiliaryAddEdit',
  props: {
    type: {
      type: String,
      default: ''
    },
    editData: {
      type: Object,
      default: () => ({})
    },
    leftData: {
      default: () => ({})
    }
  },
  data () {
    return {
      autosize: {
        minRows: 4,
        maxRows: 6
      },
      btnLoading: false,
      obj: {
        delimitationControlWord: '',
        isEnable: false,
        orderNo: '',
        remark: ''
      },
      rules: {
        delimitationControlWord: [
          {
            required: true,
            message: '请输入划控词',
            trigger: 'blur'
          }
        ],
        orderNo: [
          {
            required: true,
            message: '请输入排序号',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    ruleForm () {
      return this.editData.delimitationControlWord ? this.editData : this.obj
    }
  },
  methods: {
    handleSave () {
      // 保存
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.btnLoading = true
          if (this.type === 'add') { this.ruleForm.pid = this.leftData.id || 0 }
          const request = this.type === 'add' ? addAuxiliary(this.ruleForm) : editAuxiliary(this.ruleForm)
          request.then(() => {
            this.$message.success(this.type === 'add' ? '新增成功！' : '修改成功！')
            this.btnLoading = false
            this.$emit('handleSuccess')
          }).catch(() => {
            this.btnLoading = false
          })
        } else {
          return false
        }
      })
    },
    handleCancel () {
      // 关闭弹窗清空内容
      /* Object.keys(this.ruleForm).forEach((key) => {
              this.ruleForm[key] = ''
            }) */
      this.$emit('handleCancel')
    }
  }
}
</script>

<style lang="scss" scoped>
  .add-edit {
    padding-right: 40px;
  }
</style>
